const express = require("express");
const path = require("path");
const usersModel = require("./models");
let app = express();
// const messageModel = require("./models");  // 操作数据库的模型
app.get("/", (req, res) => {
  // 加载留言板页面
  res.sendFile(path.join(__dirname, "./views/index.html"));
});

app.get("/getusers", async (req, res) => {
  // 接收前端传递的age参数，更加age参数 获取不同的数据
  let { age, gender } = req.query;
  let where; // 查询条件的对象，默认是undefined
  if (age == 0) {
    where = { age: 1 };
  } else if (age == 1) {
    where = { age: -1 };
  }

  let genderWhere; // 筛选性别的条件，默认是undefined
  if (gender == 0) {
    genderWhere = {
      gender: "男",
    };
  } else if (gender == 1) {
    genderWhere = {
      gender: "女",
    };
  }

  let usersData = await usersModel.find(genderWhere).sort(where);
  res.json(usersData);
});

app.listen(8989);
